<template>
  <div>
  <!-- 一条文章单元格 -->
    <van-cell>
      <!-- 标题区域的插槽 -->
      <template #title>
        <div class="title-box">
          <!-- 标题 -->
          <span>{{art.title}}</span>
          <!-- 单图 -->
          <van-image class="thumb" lazy-load :src="art.cover.images[0]" v-if="art.cover.type === 1">
            <template v-slot:error>加载失败</template>
          </van-image>
        </div>
        <!-- 三张图片 -->
        <div class="thumb-box" v-if="art.cover.type > 1">
          <van-image lazy-load class="thumb" v-for="(imgUrl, index) in art.cover.images" :key="index" :src="imgUrl">
            <template v-slot:error>加载失败</template>
          </van-image>
        </div>
      </template>
      <!-- label 区域的插槽 -->
      <template #label>
        <div class="label-box">
          <div>
            <!-- <span>{{art.aut_name}}</span> -->
            <span>{{art.comm_count}}评论</span>
            <span>{{formatTime(art.pubdate)}}</span>
          </div>
          <!-- 反馈按钮 -->
          <van-icon name="cross" @click.stop="showAction"/>
        </div>
      </template>
    </van-cell>

    <!-- 反馈面板遮罩层 -->
    <van-action-sheet v-model="show" :actions="actions" @select="onSelect" :cancel-text="bottomText"
      get-container="body" @cancel="onCancel" />
  </div>
</template>

<script>
import { dislikeArticleAPI, reportArticleAPI } from '@/api'
import { firstActions, secondActions } from '@/api/report'
import { Notify, Toast } from 'vant'
import { timeAgo } from '@/utils/date'
export default {
  props: ['art'],
  data () {
    return {
      show: false, // 反馈面板的隐藏与显示
      actions: firstActions, // 反馈面板的数据
      bottomText: '取消' // 底部按钮的文字
    }
  },
  methods: {
    formatTime: timeAgo,
    // 显示遮罩层
    showAction () {
      this.show = true
      this.actions = firstActions
      this.bottomText = '取消'
    },
    // 选择选项的事件
    async onSelect (action) {
      if (action.name === '反馈垃圾内容') {
        this.actions = secondActions
        this.bottomText = '返回'
      } else if (action.name === '不感兴趣') {
        await dislikeArticleAPI(this.art.art_id)
        Notify({
          type: 'success',
          message: '反馈成功'
        })
        this.show = false
      } else {
        await reportArticleAPI(this.art.art_id, action.value)
        Notify({
          type: 'success',
          message: `${action.name} 反馈成功`
        })
        this.show = false
      }
    },
    // 取消与返回按钮之间的逻辑
    onCancel () {
      if (this.bottomText === '返回') {
        this.show = true
        this.actions = firstActions
        this.bottomText = '取消'
      } else {
        this.show = false
        Toast('取消成功')
      }
    }
  }
}
</script>

<style scoped lang="less">
  /* 标题样式 */
  .title-box {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
  }

  /* label描述样式 */
  .label-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  /* 文章信息span */
  .label-box span {
    margin: 0 3px;

    &:first-child {
      margin-left: 0;
    }
  }

  /* 图片的样式, 矩形黄金比例：0.618 */
  .thumb {
  width: 113px;
  height: 70px;
  background-color: #f8f8f8;
  object-fit: cover;
  }

  /* 三图, 图片容器 */
  .thumb-box {
  display: flex;
  justify-content: space-between;
  }
</style>
